package com.tommy.ddp.manager
{
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.GameStatus;
	import com.tommy.ddp.events.FinderEvent;
	import com.tommy.ddp.pipeline.BlockBoxesVo;
	import com.tommy.ddp.pipeline.Pipeline;
	import com.tommy.ddp.swapHandle.AdvSwapHandler;
	
	import flash.events.Event;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import mx.utils.object_proxy;

	/**
	 * 填充管理
	 * @author hongxuan
	 * 
	 */	
	public class FillUpManager2
	{
		private static var movedPipeline:uint;
		private static var swaper:AdvSwapHandler;
		
		public function FillUpManager2()
		{
		}
		
		public static function init():void
		{
			GameStatusManager.addGameStatus(GameStatus.BEGIN_FILL,onBeginFill);
		}
		/**
		 * 开始填充
		 * 
		 */		
		private static function onBeginFill(event:Event):void
		{
			trace("准备填充……");
			
			fillUp();
			//
			/*var arr:Array = PipelineDataManager.allPipelines;
			var voArray:Array = [];
			for each(var i:Pipeline in arr)
			{
				var vect:Vector.<BlockBoxesVo> = i.blockBoxesList;
				for each(var o:BlockBoxesVo in vect)
				{
					voArray.push(o);
				}
			}
			voArray.sortOn("row",Array.NUMERIC|Array.DESCENDING);
			
			//return;
			for each(var vo:BlockBoxesVo in voArray)
			{
				var boxVect:Vector.<DdpBox> = vo.boxes;
				for each(var b:DdpBox in boxVect)
				{
					b.findRoad();
				}
			}
			
			setTimeout(function():void
			{
			var arr:Array = PipelineDataManager.allPipelines;
			for each(var i:Pipeline in arr)
			{
				i.fillUpGridOfRoad();
			}
			},1000);*/
		}
		
		private static function fillUp():void
		{
			movedPipeline = 0;
			
			var arr:Array = PipelineDataManager.allPipelines;
			
			for(var i:int = PipelineDataManager.maxLength - 1;i>=0;i--)
			{
				var vect:Vector.<DdpBox> = new Vector.<DdpBox>();
				for each(var j:Pipeline in arr)
				{
					vect.push(j.getBox(i));
				}
				
				for each(var b:DdpBox in vect)
				{
					//先找好路，确定好每个管道里每个棋子最后的归属再决定每个管道创建多少个补充
					if(b)
					{
						b.findRoad();
					}
				}
			}
			//
			for each(var p:Pipeline in arr)
			{
				p.addEventListener(Pipeline.MOVE_OVER,onPipeLineMoveOver);
				p.fillUpGridOfRoad();
			}
			setTimeout(function():void
			{
			
				for each(var p:Pipeline in arr)
				{
					p.move();
				}
				
			},1000);
		}
		
		private static function onPipeLineMoveOver(event:Event):void
		{
			var p:Pipeline = event.currentTarget as Pipeline;
			p.removeEventListener(Pipeline.MOVE_OVER,onPipeLineMoveOver);
			
			movedPipeline++;
			if(movedPipeline == PipelineDataManager.allPipelines.length)
			{
				trace("全移动完了");
				setTimeout(function():void
				{
				if(!swaper)
				{
					swaper = new AdvSwapHandler();
					swaper.addEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
					/*swaper.dispose();
					swaper.removeEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);*/
				}
				swaper.execute();
				},1000);
			}
		}
		
		private static function onNoneToDispose(event:FinderEvent):void
		{
			trace("检查完毕，无可消除内容，检查是否有小炸弹");
			if(DisposeBoxManager.boomNum == 0)
			{
				trace("无小炸弹");
				MainManager.game.container.mouseEnabled = true;
			}
			else
			{
				MainManager.game.tips = "3秒后清除小炸弹……";
				trace("3秒后清除小炸弹……");
				setTimeout(function():void
				{
					DisposeBoxManager.clearBoom();
				},3000);
			}
		}
	}
}